package com.ruoyi.web.controller.lingshui.decisupport;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.supervise.domain.decisupport.EchartEntity;
import com.ruoyi.supervise.domain.decisupport.MunicipalEngineering;
import com.ruoyi.supervise.domain.decisupport.MyBigDataProZS;
import com.ruoyi.supervise.domain.decisupport.ProportionOfProjectType;
import com.ruoyi.supervise.service.decisupport.MunicipalEngineeringService;
import com.ruoyi.supervise.service.decisupport.MyBigDataProZSService;
import com.ruoyi.supervise.service.decisupport.ProportionOfProjectTypeService;
import com.ruoyi.supervise.util.DateUtils;

import cn.hutool.core.date.DateUtil;

/**
 * @ClassName: MunicipalEngineeringController
 * @Description: 市政工程开工情况Controller
 * @Author wtj
 * @DateTime 2020年1月15日 下午4:58:27
 */
@Controller
@RequestMapping("/municipalEngineering")
public class MunicipalEngineeringController {
	
	
	@Autowired
	private MunicipalEngineeringService municipalEngineeringService;
	
	@Autowired
	private ProportionOfProjectTypeService proportionOfProjectTypeService;
	
	@Autowired
	private MyBigDataProZSService myBigDataProZSService;
	
	
	/**
	 * @Title: hello
	 * @Description: 
	 * @Author wtj
	 * @DateTime 2020年1月15日 下午4:55:04
	 * @return
	 */
	@GetMapping("/analysis")
	public String hello() {
		return "decisionSupport/municipalEngineering/page.html";
	}
	
	/**
	 * @Title: indexMETable
	 * @Description: 获取市政工程开工情况详细数据table
	 * @Author wtj
	 * @DateTime 2020年1月16日 上午9:08:37
	 * @param me
	 * @return
	 */
	@GetMapping("/indexmetable")
	@ResponseBody
	public List<MunicipalEngineering> indexMETable(MunicipalEngineering me) 
	{
//		me.setTimeNowBegin("2018-01");
//		me.setTimeNowEnd("2020-01");
//		me.setTimeLastBegin("2017-01");
//		me.setTimeLastEnd("2019-01");
//		me.setIsPrivateConstruction("1");
		MunicipalEngineering met=processingTime(me);
		List<MunicipalEngineering> allME=municipalEngineeringService.selectAllProMETbale(met);
		MunicipalEngineering allMEProvince=municipalEngineeringService.selectAllProMEProvince(met);
		allMEProvince.setProjectCityNum("全省");
		List<MunicipalEngineering> allTable=new ArrayList<MunicipalEngineering>();
		for(int i=0;i<allME.size()+1;i++)
		{
			if(i==0)
			{
				allTable.add(allMEProvince);
			}else
			{
				allTable.add(allME.get(i-1));
			}
		}
		return allTable;
	}
	
	/**
	 * @Title: compareProMoneyME
	 * @Description: 获取右下角市政工程投资规模占比的数据
	 * @Author wtj
	 * @DateTime 2020年1月16日 上午9:09:04
	 * @param me
	 * @return
	 */
	@GetMapping("/compareproarea")
	@ResponseBody
	public Map<String,Object> compareProMoneyME(MunicipalEngineering me) 
	{
//		me.setTimeNowBegin("2018-01");
//		me.setTimeNowEnd("2020-01");
//		me.setTimeLastBegin("2017-01");
//		me.setTimeLastEnd("2019-01");
//		me.setIsPrivateConstruction("1");
		MunicipalEngineering met=processingTime(me);
		met.setProjectCityNum("陵水县");
		int inOneKW=municipalEngineeringService.countFJMoneyInOneKW(met);
		int oneKWToFiveKW=municipalEngineeringService.countFJMoneyOneKWToFiveKW(met);
		int fiveKWToOneB=municipalEngineeringService.countFJMoneyFiveKWToOneB(met);
		int oneBToFiveB=municipalEngineeringService.countFJMoneyOneBToFiveB(met);
		int upFiveB=municipalEngineeringService.countFJMoneyUpFiveB(met);
		Map<String,Object> compareProMoneyMEMap=new HashMap<String,Object>();
		String[] strArr= {"1千万以内","1千万-5千万","5千万-1亿","1亿-5亿","5亿以上"};
		int[] iArr= {inOneKW,oneKWToFiveKW,fiveKWToOneB,oneBToFiveB,upFiveB};
		compareProMoneyMEMap.put("y", strArr);
		compareProMoneyMEMap.put("x", iArr);
		return compareProMoneyMEMap;
	}
	
	/**
	 * @Title: compareMEProType
	 * @Description: 各类型市政项目占比饼图
	 * @Author wtj
	 * @DateTime 2020年1月16日 上午9:26:06
	 * @param me
	 * @return
	 */
	@GetMapping("/comparemeprotype")
	@ResponseBody
	public List<ProportionOfProjectType> compareMEProType(MunicipalEngineering me)
	{
//		me.setTimeNowBegin("2018-01");
//		me.setTimeNowEnd("2020-01");
//		me.setTimeLastBegin("2017-01");
//		me.setTimeLastEnd("2019-01");
//		me.setIsPrivateConstruction("1");
		MunicipalEngineering met=processingTime(me);
		met.setProjectCityNum("陵水县");
		List<ProportionOfProjectType> pptList=proportionOfProjectTypeService.MEProportionOfProjectTypeBT(met);
		return pptList;
	}
	
	/**
	 * @Title: startAllMENumMonthZST
	 * @Description: 右上角全省市政开工月度趋势走势图
	 * @Author wtj
	 * @DateTime 2020年1月16日 上午10:31:31
	 * @param me
	 * @return
	 */
	@GetMapping("/startallmenummonthzst")
	@ResponseBody
	public Map<String,Object> startAllMENumMonthZST(MunicipalEngineering me)
	{
//		me.setTimeNowBegin("2018-01");
//		me.setTimeNowEnd("2020-01");
//		me.setTimeLastBegin("2017-01");
//		me.setTimeLastEnd("2019-01");
//		me.setIsPrivateConstruction("1");
		MunicipalEngineering met=processingTime(me);
		met.setProjectCityNum("陵水县");
		List<MyBigDataProZS> allProvinceMEStartProNumList=myBigDataProZSService.selectMEProNumMonthZSData(met);
		Map<String,Object> allProvinceMEStartProNumMap=new HashMap<String,Object>();
		String[] monthArr=new String[allProvinceMEStartProNumList.size()];
		String[] numTotalArr=new String[allProvinceMEStartProNumList.size()];
		String[] numTotalCompareArr=new String[allProvinceMEStartProNumList.size()];
		for(int i=0;i<allProvinceMEStartProNumList.size();i++)
		{
			monthArr[i] = String.valueOf(allProvinceMEStartProNumList.get(i).getMonth());
			numTotalArr[i]=allProvinceMEStartProNumList.get(i).getTotal();
			numTotalCompareArr[i]=allProvinceMEStartProNumList.get(i).getCompare();
		}
		allProvinceMEStartProNumMap.put("month", monthArr);
		allProvinceMEStartProNumMap.put("numTotalArr", numTotalArr);
		allProvinceMEStartProNumMap.put("numTotalCompareArr", numTotalCompareArr);
		return allProvinceMEStartProNumMap;
	}
	
	/**
	 * @Title: startAllMEMoneyMonthZST
	 * @Description: 投资金额月度趋势走势图
	 * @Author wtj
	 * @DateTime 2020年1月16日 上午10:32:03
	 * @param me
	 * @return
	 */
	@GetMapping("/startallmemoneymonthzst")
	@ResponseBody
	public Map<String,Object> startAllMEMoneyMonthZST(MunicipalEngineering me)
	{
//		me.setTimeNowBegin("2018-01");
//		me.setTimeNowEnd("2020-01");
//		me.setTimeLastBegin("2017-01");
//		me.setTimeLastEnd("2019-01");
//		me.setIsPrivateConstruction("1");
		MunicipalEngineering met=processingTime(me);
		met.setProjectCityNum("陵水县");
		List<MyBigDataProZS> allProvinceMEStartProNumList=myBigDataProZSService.selectMEProMoneyMonthZSData(met);
		Map<String,Object> allProvinceMEStartProMoneyMap=new HashMap<String,Object>();
		String[] monthArr=new String[allProvinceMEStartProNumList.size()];
		String[] moneyTotalArr=new String[allProvinceMEStartProNumList.size()];
		String[] moneyTotalCompareArr=new String[allProvinceMEStartProNumList.size()];
		for(int i=0;i<allProvinceMEStartProNumList.size();i++)
		{
			monthArr[i] = String.valueOf(allProvinceMEStartProNumList.get(i).getMonth());
			moneyTotalArr[i]=allProvinceMEStartProNumList.get(i).getTotal();
			moneyTotalCompareArr[i]=allProvinceMEStartProNumList.get(i).getCompare();
		}
		allProvinceMEStartProMoneyMap.put("month", monthArr);
		allProvinceMEStartProMoneyMap.put("moneyTotalArr", moneyTotalArr);
		allProvinceMEStartProMoneyMap.put("moneyTotalCompareArr", moneyTotalCompareArr);
		return allProvinceMEStartProMoneyMap;
	}
	
	/**
	 * @Title: startNumRLTME
	 * @Description: 市政工程开工情况开工数量热力图数据
	 * @Author wtj
	 * @DateTime 2020年1月16日 上午10:40:40
	 * @param me
	 * @return
	 */
	@GetMapping("/startnumrltme")
	@ResponseBody
	public List<EchartEntity> startNumRLTME(MunicipalEngineering me)
	{
//		me.setTimeNowBegin("2018-01");
//		me.setTimeNowEnd("2020-01");
//		me.setTimeLastBegin("2017-01");
//		me.setTimeLastEnd("2019-01");
//		me.setIsPrivateConstruction("1");
		MunicipalEngineering met=processingTime(me);
		List<MunicipalEngineering> allMENumData=municipalEngineeringService.selectAllNumMERLT(met);
		String yp="0";
		for(MunicipalEngineering meyp:allMENumData)
		{
			if(meyp.getProjectCityNum().equals("洋浦经济开发区"))
			{
				yp=meyp.getTotalNum();
			}
		}
		for(MunicipalEngineering myme:allMENumData)
		{
			if(myme.getProjectCityNum().equals("儋州市"))
			{
				int dzLastInt=Integer.parseInt(myme.getTotalNum());
				int ypInt=Integer.parseInt(yp);
				int dzNowInt=dzLastInt+ypInt;
				myme.setTotalNum(String.valueOf(dzNowInt));
			}
		}
		List<EchartEntity> meNumList=new ArrayList<EchartEntity>();
		for(MunicipalEngineering m:allMENumData)
		{
			EchartEntity ee=new EchartEntity();
			ee.setName(m.getProjectCityNum());
			ee.setValue(m.getTotalNum());
			meNumList.add(ee);
		}
		return meNumList;
	}
	
	@GetMapping("/hainan.json")
	public String haiNanJson(Model model) {
		return "decisionSupport/municipalEngineering/hainan.json";
	}
	
	public MunicipalEngineering processingTime(MunicipalEngineering me)
	{
		//对时间进行处理
		DateUtils du=new DateUtils();
		Calendar calendarBegin = Calendar.getInstance();
		Calendar calendarEnd = Calendar.getInstance();
		if(StringUtils.isAnyBlank(me.getTimeNowBegin(),me.getTimeNowEnd()))
		{
			Date beginDayOfNowYear=du.getBeginDayOfYear();//获取当前年的第一天
			Date endDayOfNowYear=du.getEndDayOfYear();//获取当前年的最后一天
			calendarBegin.setTime(beginDayOfNowYear);
			calendarBegin.add(Calendar.YEAR,-1);
			Date beginDayOfLastYear=calendarBegin.getTime();//获取特定时间的去年时间
			calendarEnd.setTime(endDayOfNowYear);
			calendarEnd.add(Calendar.YEAR,-1);
			Date endDayOfLastYear=calendarEnd.getTime();//获取特定时间的去年时间
			SimpleDateFormat formatterNow2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			me.setTimeNowBegin(formatterNow2.format(beginDayOfNowYear));
			me.setTimeNowEnd(formatterNow2.format(endDayOfNowYear));
			me.setTimeLastBegin(formatterNow2.format(beginDayOfLastYear));
			me.setTimeLastEnd(formatterNow2.format(endDayOfLastYear));
		}else
		{	
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date dateNowBegin = DateUtil.parse(me.getTimeNowBegin(), "yyyy-MM");
			Date dateNowBeginDay=DateUtil.beginOfMonth(dateNowBegin);
			me.setTimeNowBegin(formatter.format(DateUtil.beginOfMonth(dateNowBeginDay)));
			calendarBegin.setTime(dateNowBeginDay);
			calendarBegin.add(Calendar.YEAR,-1);
			Date beginDayOfLastYearMonth=calendarBegin.getTime();
			me.setTimeLastBegin(formatter.format(DateUtil.beginOfMonth(beginDayOfLastYearMonth)));
			Date dateNowEnd = DateUtil.parse(me.getTimeNowEnd(), "yyyy-MM");
			Date dateNowEndDay=DateUtil.beginOfMonth(dateNowEnd);
			me.setTimeNowEnd(formatter.format(DateUtil.endOfMonth(dateNowEndDay)));
			calendarBegin.setTime(dateNowEndDay);
			calendarBegin.add(Calendar.YEAR,-1);
			Date EndDayOfLastYearMonth=calendarBegin.getTime();
			me.setTimeLastEnd(formatter.format(DateUtil.endOfMonth(EndDayOfLastYearMonth)));
		}
		return me;
	}
}
